﻿using System;
using System.Globalization;

namespace RaytracerLib.Resamplers.Filters
{
    public class MitchellFilter : Filter
    {
        public double B = 1.0 / 3.0;
        public double C = 1.0 / 3.0;

        public override string Name
        {
            get
            {
                return "Mitchell";
            }
        }

        public MitchellFilter()
            : base(2)
        { 
        }

        public override FilterType FilterType
        {
            get
            {
                return FilterType.Mitchell;
            }
        }

        public override double Evaluate(double a_value)
        {
            double v = Math.Abs(a_value);

            if (v > 2)
                return 0;

            double v2 = v * v;

            if (v > 1)
            {
                return (1.0 / 6.0) * (
                    (-B - 6 * C) * v2 * v +
                    (6 * B + 30 * C) * v2 +
                    (-12 * B - 48 * C) * v +
                    8 * B + 24 * C);
            }
            else
            {
                return (1.0 / 6.0) * (
                    (12 - 9 * B - 6 * C) * v2 * v +
                    (12 * B + 6 * C - 18) * v2 +
                    6 - 2 * B);
            }
        }

        public override string ToString()
        {
            return String.Format(CultureInfo.InvariantCulture, "Mitchell, B: {0}, C: {1}", B, C);
        }
    }
}